package com.hishop.controller.backend;

import com.hishop.common.ServerResponse;
import com.hishop.pojo.Category;
import com.hishop.service.ICategoryService;
import com.hishop.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by hejie on 2017/7/16.
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 增加分类节点
     * @param session
     * @param categoryName
     * @param parentId
     * @return
     */
    @RequestMapping(value = "add_category.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") int parentId){
       /* User user= (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if (!iUserService.checkAdminRole(user).isSuccess()){
            return ServerResponse.createByErrorMessage("需要管理员权限操作");
        }*/
        return iCategoryService.addCategory(categoryName,parentId);
    }

    /**
     * 修改分类名字
     * @param session
     * @param categoryId
     * @param categoryName
     * @return
     */
    @RequestMapping(value="set_category_name.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session, Integer categoryId, String categoryName){
        /*User user= (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if (!iUserService.checkAdminRole(user).isSuccess()){
            return ServerResponse.createByErrorMessage("需要管理员权限操作");
        }*/
        return iCategoryService.updateCategoryName(categoryId,categoryName);
    }

    /**
     * 获取子节点平级信息
     * @param categoryId
     * @return
     */
    @RequestMapping(value="get_children_parallel_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<List<Category>> getChildrenParallelCategory(HttpSession session, @RequestParam(value="categoryId",defaultValue = "0") Integer categoryId){
       /* User user= (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if (!iUserService.checkAdminRole(user).isSuccess()){
            return ServerResponse.createByErrorMessage("需要管理员权限操作");
        }*/
            return iCategoryService.getChildrenParallelCategory(categoryId);
    }

    @RequestMapping(value="get_deep_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<List<Category>> getCategoryAndDeepChildrenCategory(HttpSession session, @RequestParam(value="categoryId",defaultValue = "0") Integer categoryId){
       /* User user= (User) session.getAttribute(Constant.CURRENT_USER);
        if (user==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请登录");
        }
        //校验是否是管理员
        if (!iUserService.checkAdminRole(user).isSuccess()){
            return ServerResponse.createByErrorMessage("需要管理员权限操作");
        }*/
        return iCategoryService.getCategoryAndDeepChildrenCategory(categoryId);
    }

}
